<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd" style="margin-right: 5px">新增</a-button>
        <ExcelButton :config="excelConfig" />
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="i-ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="i-mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableActions(record)" />
      </template>
    </BasicTable>
    <!-- <a-modal v-model:open="isShowCopyModal" title="请输入复制后的模板名称" @ok="handlerDoCopy">
      <a-input v-model:value="copyGroupName" placeholder="名称" />
    </a-modal> -->
    <TemplateCopyModal @register="registerCopyModal" @success="handleTemplateCopySuccess" />
    <TemplateModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="gen-code-template" setup>
  import { ref, unref, onMounted } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useModal } from "/@/components/Modal"
  import TemplateCopyModal from "./TemplateCopyModal.vue"
  import ExcelButton from "/@/components/My/ExcelButton.vue"
  import { pageTemplate, deleteTemplate, batchDeleteTemplate, getExportUrl, getImportUrl } from "./template.api"
  import { listTemplateGroup } from "../templateGroup/templateGroup.api"
  import { columns, searchFormSchema } from "./template.data"
  import TemplateModal from "./TemplateModal.vue"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { Icon } from "/@/components/Icon"

  const { createMessage } = useMessage()
  const [registerModal, { openModal }] = useModal()
  const [registerCopyModal, { openModal: openCopyModal }] = useModal()

  const currentGroupId = ref("")

  const excelConfig = {
    exportConfig: {
      name: "代码模板",
      url: () =>  `${getExportUrl}?groupId=${currentGroupId.value}`,
    },
    importConfig: {
      showModal: true,
      url: () => `${getImportUrl}?groupId=${currentGroupId.value}`,
      fail: () => {
        // createMessage.destroy('template_import_config');
      },
      success: () => reload(),
    },
  }
  const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
    designScope: "gen-code-template",
    tableProps: {
      title: "代码模板列表",
      api: pageTemplate,
      columns: columns,
      immediate: false,
      formConfig: {
        labelWidth: 80,
        schemas: searchFormSchema,
        fieldMapToTime: [["fieldTime", ["beginDate", "endDate"], "YYYY-MM-DD HH:mm:ss"]],
      },
      bordered: false,
      actionColumn: {
        width: 140,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
    },
  })

  const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext

  //在组件被挂载之后调用(vue3)
  onMounted(async () => {
    //将数据放入下拉框中
    const { updateSchema, setFieldsValue } = getForm()
    const option = await listTemplateGroup({})
    await updateSchema({
      field: "groupId",
      componentProps: {
        optionFilterProp: "label",
        showSearch: true,
        options: unref(option),
        onSelect: (value: any, option: any) => {
          console.log("option", option)
          currentGroupId.value = option.value
          reload()
        },
      },
    })
    currentGroupId.value = option.length > 0 ? option[0].id : null
    setFieldsValue({ groupId: currentGroupId.value })
    reload()
  })

  /**
   * 操作列定义
   * @param record
   */
  function getTableActions(record) {
    return [
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "复制",
        popConfirm: {
          title: "是否确认复制模板",
          confirm: handleCopy.bind(null, record),
        },
      },
      {
        label: "删除",
        color: "error",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    })
  }

  /**
   * 编辑事件
   */
  function handleCopy(record) {
    openCopyModal(true, record)
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    })
  }
  function handleTemplateCopySuccess() {
    reload()
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteTemplate({ id: record.id }, reload)
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteTemplate({ ids: selectedRowKeys.value }, reload)
  }
</script>
